package org.team751.sensors;

import edu.wpi.first.wpilibj.AnalogChannel;
import edu.wpi.first.wpilibj.SensorBase;
import edu.wpi.first.wpilibj.parsing.ISensor;

/**
 * Handles analog input from the temperature sensor integrated into the ADW22307 gyroscope sensor
 * @author Sam Crow
 */
public class GyroTemperatureSensor extends AnalogChannel implements ISensor {

    /** The voltage that the sensor outputs while at the base temperature */
    private static final double kBaseVoltage = 2.5;
    /** The temperature at which the sensor outputs the base voltage */
    private static final int kBaseTemperature = 25;
    /** The number of volts that the sensor output changes for every degree of temperature difference from the base temperature */
    private static final double kVoltsPerDegree = 0.009;

    /**
     * Constructor with a given module slot and analog channel
     * @param slot The cRIO slot that the analog module is attached to
     * @param channel The analog channel that the sensor is connected to
     */
    public GyroTemperatureSensor(int slot, int channel){
        super(slot, channel);
        init();
    }

    /**
     * Constructor with the default module slot and a given channel
     * @param channel The analog channel that the sensor is connected to
     */
    public GyroTemperatureSensor(int channel){
        super(channel);
        init();
    }

    /**
     * Set up averaging/oversampling as desired for use with this sensor
     */
    private void init(){
        setAverageBits(10);
    }

    /**
     * Get the temperature sensed by this sensor
     * @return The temperature, in degrees celsius, that the sensor measures
     */
    public double getTemperature(){
        double volts = getAverageVoltage();
        volts -= kBaseVoltage;
        double degrees = kBaseTemperature + volts / kVoltsPerDegree;
        return degrees;
    }
}
